package com.szsfm.vehiclemanagement.adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.bumptech.glide.Glide;
import com.szsfm.vehiclemanagement.constant.Config;
import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.bean.VehicleImage;
import com.szsfm.vehiclemanagement.viewholder.ImageHolder;

import java.util.List;

/**
 * Created by zhoubigbo on 2017/11/8.
 */
public class VehicleImageAdapter extends RecyclerView.Adapter<ImageHolder> implements View.OnClickListener, View.OnLongClickListener, Config {

    private Context context;
    private LayoutInflater inflater;
    private List<VehicleImage> vehicleImageList;
    private OnItemClickListener listener;
    private OnItemLongClickListener itemLongClickListener;
    VehicleImage addBean = new VehicleImage();


    public VehicleImageAdapter(Context context, List<VehicleImage> lst) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.vehicleImageList = lst;
        addBean.setButtonType(VehicleImage.BUTTON_TYPE_ADD);
        this.vehicleImageList.add(addBean);
    }

    @Override
    public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_img_recycle_view, parent, false);
        ImageHolder holder = new ImageHolder(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ImageHolder holder, int position) {
        VehicleImage bean = vehicleImageList.get(position);
        Uri uri = bean.getUri();
        if (uri != null) {
            Glide.with(context).load(uri).into(holder.imageView);
        } else {
            holder.imageView.setImageResource(VehicleImage.DEF_ADD_IMG_ID);
        }
        if (bean.getButtonType() == VehicleImage.BUTTON_TYPE_IMG) {
            holder.deleteButton.setVisibility(View.VISIBLE);
            holder.deleteButton.setOnClickListener(this);
            holder.deleteButton.setTag(bean);
            holder.border.setBackgroundColor(context.getResources().getColor(R.color.contentViewBackground));
        } else {
            if (vehicleImageList.size() == MAX_UP_IMG_SIZE) {
                holder.border.setVisibility(View.GONE);
            } else {
                holder.border.setVisibility(View.VISIBLE);
                holder.border.setBackgroundColor(Color.WHITE);
            }
            holder.deleteButton.setVisibility(View.GONE);
        }
        holder.itemView.setTag(bean);
    }

    @Override
    public int getItemCount() {
        return this.vehicleImageList.size();
    }

    @Override
    public void onClick(View view) {
        if (view instanceof Button) {
            VehicleImage item = (VehicleImage) view.getTag();
            deleteItem(item);
        } else {
            if (this.listener != null) this.listener.onItemClick(view, (VehicleImage) view.getTag());
        }

    }

    @Override
    public boolean onLongClick(View view) {
        if (this.itemLongClickListener != null) this.itemLongClickListener.onItemLongClick(view, (VehicleImage) view.getTag());
        return false;
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        this.listener = l;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener l) {
        this.itemLongClickListener = l;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, VehicleImage bean);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, VehicleImage bean);
    }

    public void addItem(VehicleImage item) {
        vehicleImageList.add(0, item);
        notifyItemInserted(0);
        notifyDataSetChanged();
    }

    public void deleteItem(VehicleImage item) {
        int position = this.vehicleImageList.indexOf(item);
        this.vehicleImageList.remove(item);
         notifyItemRemoved(position);
        notifyDataSetChanged();
    }
}
